package customview.ike.com.customviewproject.widget.Refresh;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;



/**
 *@author:ike
 *@function:
 *@creat time:11:26 2018/3/28
 **/

public class PullAbleRecyclerView extends RecyclerView implements PullAbleView{
    public int  top=0;
    private boolean PullDown=true;
    private boolean PullUp=true;

    public PullAbleRecyclerView(Context context) {
        super(context);
    }

    public PullAbleRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public PullAbleRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean canPullDown() {
        if (!PullDown){
            return false;
        }
        if (getAdapter()==null){
            if (getParent() instanceof PullToRefreshLayout){
                PullToRefreshLayout parent= (PullToRefreshLayout) getParent();
                int scrollY = parent.getScrollY();
                if (scrollY==0){
                    return true;
                }
            }
        }else {
            LinearLayoutManager manager= (LinearLayoutManager) getLayoutManager();
            int firstVisibleItemPosition = manager.findFirstVisibleItemPosition();
            View firstChild = getChildAt(0);
            
            if (firstVisibleItemPosition==0&&firstChild.getTop()==top){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean canPullUp() {
        if (!PullUp){
            return false;
        }
        if (getAdapter()==null){
            return false;
        }
        boolean viewAtBottom = Utils.isViewAtBottom(this);
        if (viewAtBottom){
            return true;
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        switch (e.getAction()){
            case MotionEvent.ACTION_MOVE:
                Adapter adapter = getAdapter();
//                if (adapter instanceof JourneySnapShortAdapter){
//                    JourneySnapShortAdapter jAdapter= (JourneySnapShortAdapter) adapter;
//                    jAdapter.closeTvDelete();
//                }
                break;
            default:
                break;
                   }
        return super.onTouchEvent(e);
    }
    public void setCanPullDown(boolean canPullDown){
        this.PullDown=canPullDown;
        
    }
    public void setCanPullUp(boolean canPullUp){
        this.PullUp=canPullUp;

    }
}
